package com.lepu.app.fun.hospital.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.HeadIconLoadUtil;
import com.app.utils.LoginUtil;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.chat.activity.OnlineActivity;
import com.lepu.app.fun.chat.business.uiflow.UITransition;
import com.lepu.app.fun.hospital.bean.HospitalDoctor;

import java.util.ArrayList;

public class DoctorListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<HospitalDoctor> mArrayList = null;

	public DoctorListAdapter(Context context,
			ArrayList<HospitalDoctor> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.mother_item_doctor,
					null);
			holder = new ViewHolder();
			holder.txt_user_name = (TextView) convertView
					.findViewById(R.id.txt_user_name);
			holder.txt_hospital = (TextView) convertView
					.findViewById(R.id.txt_hospital);
			holder.txt_goodat = (TextView) convertView
					.findViewById(R.id.txt_goodat);
			holder.txtviewChat = (TextView) convertView
					.findViewById(R.id.txtview_chat);
			holder.txt_teachtitle = (TextView) convertView
					.findViewById(R.id.txt_teachtitle);
			holder.ic_img = (ImageView) convertView.findViewById(R.id.ic_img);
			holder.layout_more = (LinearLayout) convertView
					.findViewById(R.id.layout_more);

			TextView txt_score = (TextView) convertView
					.findViewById(R.id.txt_score);
			holder.txt_score = txt_score;

			TextView txt_score_old = (TextView) convertView
					.findViewById(R.id.txt_score_old);
			TextView txt_online_state = (TextView) convertView
					.findViewById(R.id.txt_online_state);

			holder.txt_score_old = txt_score_old;
			holder.txt_online_state = txt_online_state;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final HospitalDoctor item = mArrayList.get(position);
		holder.txt_user_name.setText(item.DoctorName);
		holder.txt_hospital.setText(item.HospitalName);
		if (!TextUtils.isEmpty(item.JobTitle)) {
			holder.txt_teachtitle.setText(item.JobTitle);
		}
		holder.txt_goodat.setText("擅长：" + item.Skill);
		holder.txtviewChat
				.setBackgroundResource(item.IsOnDuty == 1 ? R.drawable.hospital_ic_online
						: R.drawable.hospital_ic_offline);
		holder.ic_img.setImageResource(R.drawable.app_ic_default_avatar);
//		UniversalImageLoadTool.disPlay(item.Avatar, holder.ic_img,
//				R.drawable.app_ic_default_avatar);
		LoginUtil.loadSelfUserImage(mContext, holder.ic_img, item.Avatar);
		holder.layout_more.setVisibility(View.GONE);
		// if (position == mArrayList.size() - 1) {
		// holder.layout_more.setVisibility(View.VISIBLE);
		// }
		// holder.layout_more.setOnClickListener(new View.OnClickListener() {
		// @Override
		// public void onClick(View view) {
		// //todo
		// }
		// });

		// holder.titleTextView.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // Intent intent = new
		// Intent(mContext,HospitalConsultDetailActivity.class);
		// // intent.putExtra("question", item.ARQuestion);
		// // intent.putExtra("answer", item.ARAnswer);
		// // ((BaseFragmentActivity)mContext).startActivity(intent, true);
		// }
		// });
		holder.txtviewChat.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (item.IsOnDuty == 1) {
					sendMesage(item);
				} else {
					UIHelper.showToast(mContext, "该医生当前不在线，请选择其他医生咨询");
				}
			}
		});

		holder.txt_score_old.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

		holder.txt_score.setText(String.format("%d金币/次", item.ConsultCredits));
		holder.txt_score_old.setText(String.format("原价：%.0f金币/次",
				item.MarketConsultCredits));

		if (item.IsOnDuty == 1) {
			holder.txt_online_state.setText("出诊");
			holder.txt_online_state.setTextColor(mContext.getResources()
					.getColor(R.color.color_text_highlight));
		} else {
			holder.txt_online_state.setText("休息");
			holder.txt_online_state.setTextColor(Color.parseColor("#969696"));
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView txt_user_name;
		TextView txt_hospital;
		TextView txt_goodat;
		TextView txtviewChat;
		TextView txtIsOnDuty, txt_teachtitle;
		ImageView ic_img;
		LinearLayout layout_more;

		TextView txt_score;
		TextView txt_score_old;
		TextView txt_online_state;
	}

	private void sendMesage(HospitalDoctor doctor) {
		boolean flag = LoginUtil.jumpLogin((BaseFragmentActivity) mContext);
		if (flag) {
			return;
		}

		// // TODO: 2015/12/8 新需求 未确认
		Intent intent = new Intent(mContext, OnlineActivity.class);
		intent.putExtra("doctorEntity", doctor);
		intent.putExtra("ConsultPrice", doctor.ConsultCredits);
		mContext.startActivity(intent);
		UITransition.setActivityStartRightIn((FragmentActivity) mContext);

	}
}
